package kongkong;

import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * 
 * @DESCRIPTION:图片工具类
 * @TODO:未实现的事情
 * @author xiaokong
 * @date 2021年5月13日
 * @version 1.0.0
 * @file com.frame.util.ImageUtil.java
 */
public class ImageUtil {
	public static byte[] dealImag(InputStream in, File watermarkFile, int width, int height, float quality, float opacity, boolean forceSize) {
		if(in==null) {
			return null;
		}
		ByteArrayOutputStream out = null;
		try {
			out = new ByteArrayOutputStream();
			if(forceSize){
				if(watermarkFile==null) {
					Thumbnails.of(in).forceSize(width, height).outputQuality(quality).toOutputStream(out);
				}else {
					Thumbnails.of(in).forceSize(width, height).outputQuality(quality).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(watermarkFile), opacity).toOutputStream(out);
				}
			}else {
				if(watermarkFile==null) {
					Thumbnails.of(in).size(width, height).outputQuality(quality).toOutputStream(out);
				}else {
					Thumbnails.of(in).size(width, height).outputQuality(quality).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(watermarkFile), opacity).toOutputStream(out);
				}
			}
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(out!=null) {
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
